Spring MVC之ModelAndView分析

前台表单

<form class="form-horizontal" role="form" action="user/login" method="post">
账号:<input type="text" class="form-control" name="username" id="username">
密码:<input type="password" class="form-control" name="userpwd" id="userpwd">
<button type="submit" class="btn btn-default"> 登录</button>
</form>

controller

package com.music.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.music.model.User;
import com.music.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private User user;
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
   public ModelAndView login(String username,String userpwd,ModelAndView modelAndView) {
    Map<String,Object> map=new HashMap<>();
    map.put("username", username);
    map.put("userpwd", userpwd);
    User user=userService.selectByNameAndUserpwd(map);
    
    if (user!=null) {
        modelAndView.setViewName("success");字符串方式//视图在web-inf下面为success.jsp文件
        modelAndView.addAllObjects(map);
        return modelAndView ;
    }
    return modelAndView;
}
}
modelAndView.setViewName("/user/success");路径方式//视图在web-inf下的user文件夹下的success.jsp文件

 

 

mv.setViewName("redirect:/user/success.jsp");重定向方式//视图在web-inf下的user文件夹下的success.jsp

 

以上三种modelAndView.setViewName,都可以返回视图

如果想用ModelAndView返回,就public ModelAndView login(){}这样写方法

modelAndView.setViewName("success");跟return “success”一样,由视图解析跳转到你配置的页面,一般是   /success.jsp  这个页面

第二种

controller中的写法

package com.music.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.music.model.User;
import com.music.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private User user;
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
   public String login(String username,String userpwd) {
    Map<String,Object> map=new HashMap<>();
    map.put("username", username);
    map.put("userpwd", userpwd);
    User user=userService.selectByNameAndUserpwd(map);
    ModelAndView mv =new ModelAndView();
    if (user!=null) {
        mv.setViewName("success");
        return mv.getViewName() ;
    }
    return mv.getViewName();
}
}
这是返回的不是ModelAndView型的,但是可以把ModelAndView   new出来,直接使用。

 

主要针对新手上手代码。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值